package com.example.gyk.neteaseanimation.animation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.Nullable;

/**
 * 项目名：JetpackPractice
 * 包名：com.example.gyk.jetpackpractice.animation
 * 文件名：TextImageView
 * 创建者：Gyk
 * 创建时间：2021/8/13 16:20
 * 描述：  TODO
 */

@SuppressLint("AppCompatCustomView")
public class TextImageView extends ImageView {

    private Context mContext;
    private int width;
    private int height;
    private TextPaint mContentPaint;
    private TextPaint mBottomPaint;
    private String mContent;
    private String mBContent;
    private String mColor;
    private final String TAG = "TextImageView";

    public TextImageView(Context context) {
        this(context, null);
        this.mContext = context;
    }

    public TextImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public TextImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    public void setText(String text, String bContent) {
        this.mContent = text;
        this.mBContent = bContent;
        invalidate();
    }

    public void setNewColor(String color,String bColor) {
        this.mColor = color;
        mContentPaint.setColor(Color.parseColor(color));
        mBottomPaint.setColor(Color.parseColor(bColor));
        invalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mearsureWidth(widthMeasureSpec), mearsureHeight(heightMeasureSpec));
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        initPaint();
    }

    private void initPaint() {
        mContentPaint = new TextPaint();
        mContentPaint.setTextSize(DisplayUtil.sp2px(mContext, width / 50));
        mContentPaint.setColor(Color.parseColor("#000000"));

        mBottomPaint = new TextPaint();
        mBottomPaint.setTextSize(DisplayUtil.sp2px(mContext, width / 100));
        mBottomPaint.setColor(Color.parseColor("#b4b4b4"));
    }


    private void drawContent(Canvas canvas) {
        // 多行绘制
        StaticLayout staticLayout = new StaticLayout(
                mContent,             // 需要绘制的文本
                mContentPaint,         // 画笔对象（这里需要TextPaint）
                width-60,         // layout的宽度，文本超出宽度时自动换行
                Layout.Alignment.ALIGN_NORMAL,  // layout的对其方式
                1f,         // 文字行间距，根据文字大小比例计算 0.8f  1.2f
                0,          // 文字行间距，填写具体值
                false       // 纵向是否加额外高度

        );

        canvas.save();
        canvas.translate(60, height / 3);
        staticLayout.draw(canvas);
        canvas.restore();
    }

    private void drawBottom(Canvas canvas) {
        StaticLayout staticLayout = new StaticLayout(
                mBContent,             // 需要绘制的文本
                mBottomPaint,         // 画笔对象（这里需要TextPaint）
                width / 2,         // layout的宽度，文本超出宽度时自动换行
                Layout.Alignment.ALIGN_NORMAL,  // layout的对其方式
                1f,         // 文字行间距，根据文字大小比例计算 0.8f  1.2f
                0,          // 文字行间距，填写具体值
                false       // 纵向是否加额外高度

        );

        canvas.save();
        canvas.translate(width / 3+40, height - 200);
        staticLayout.draw(canvas);
        canvas.restore();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawContent(canvas);
        drawBottom(canvas);
    }


    private int mearsureWidth(int widthMeasureSpec) {
        int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int size;
        if (widthSpec == MeasureSpec.EXACTLY) {
            size = widthSize;
        } else {
            size = DisplayUtil.dip2px(mContext, 300);
            if (widthSpec == MeasureSpec.AT_MOST) {
                size = Math.min(size, widthSize);
            }
        }
        return size;
    }

    private int mearsureHeight(int heightMeasureSpec) {
        int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int size;
        if (heightSpec == MeasureSpec.EXACTLY) {
            size = heightSize;
        } else {
            size = DisplayUtil.dip2px(mContext, 300);
            if (heightSpec == MeasureSpec.AT_MOST) {
                size = Math.min(size, heightSize);
            }
        }
        return size;
    }

}
